import {jsCallJavaAbility} from "../../common/JsCallJavaAbilityUtils.js"

export default {
    data: {
        before_heart: [],
        after_heart:[],
        heartOps: {
            xAxis: {
                min: 0,
                max: 50,
                axisTick:2,
                display: true
            },
            yAxis: {
                min: 0,
                display: true
            },
            series: {
                lineStyle: {
                    width: "3px",
                    smooth: true
                },
                loop: {
                    margin: 2,
                    gradient: true
                }
            }
        }
    },
    computed: {
        before_heart_str() {
            return this.before_heart.join(" ")
        },
        heartData() {
            let before_heart = []
            let after_heart = []
            this.before_heart.forEach((item, index) => {
                before_heart.push({
                    value: item
                })
            })
            this.after_heart.forEach((item)=>{
                after_heart.push({
                    value:item
                })
            })
            return [{
                        strokeColor: '#0081ff',
                        data: before_heart,
                        gradient: false,
                    }, {
                        strokeColor: 'red',
                        data: after_heart,
                        gradient: false
                    }]
        },

        reliability() {//此处计划调用AI接口，暂未实现该功能。先随便写个算法
            let result=99.99;
            if(this.arrAvg(this.before_heart)<this.arrAvg(this.after_heart) && this.after_heart.length>3){
                result-=10
            }
            var bmax=Math.max.apply(null,this.before_heart);
            var amax=Math.max.apply(null,this.after_heart);
            if(bmax<amax && amax>0){
                result-=Math.random()* 20
            }
            var bmin=Math.min.apply(null,this.before_heart);
            var amin=Math.min.apply(null,this.after_heart);
            if(bmin>amin && amin>0){
                result-=Math.random()* 20;
            }
            return  result.toFixed(2);
        }
    },
    arrAvg(data){
        const len=data.length;
        var sum=0;
        data.map(t=>sum+=t)
        return sum/len
    },
    getHeartData() {
        jsCallJavaAbility.subAbility("AnswerAbility", 200, (res) => {
            let {before_heart,after_heart} = res.data;
            if (before_heart) {
                this.before_heart = JSON.parse(before_heart)
            }else{
                this.before_heart=[]
            }
            if(after_heart){
                this.after_heart=JSON.parse(after_heart)
            }else{
                this.after_heart=[]
            }
        })
    },
    onShow() {
        this.getHeartData();
    }
}
